﻿package com.mecol.hotel.web;

import com.mecol.hotel.entity.*;
import com.mecol.hotel.service.CommentService;
import com.mecol.hotel.service.ReservationService;
import com.mecol.hotel.service.RoomService;
import com.mecol.hotel.service.UserService;
import com.mecol.hotel.util.EncryptUtil;
import com.mecol.hotel.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController
{


/*



 完整项目文件数据库文件  qq 2144920175   weixin 15263495021
more。。。。

ssm医院预约系统
ssm汽车租赁系统
ssm小区物业管理系统
ssm图书馆管理系统
ssm学生成绩管理系统
ssm校园二手交易网
ssm酒店预订管理
ssm 学生宿舍管理
ssm电商花店
ssm房屋租赁系统
ssm超市管理系统
ssm学生选课系统
ssm学生信息管理系统
ssm高校选课考勤系统
ssm电影院购票系统
ssm网上书城
springboot 旅游管理系统
springboot 学生社团管理
springboot 在线考试系统
springboot 仓库管理系统
springboot 个人博客系统 



*/



    @Autowired
    private UserService userService;
    @Autowired
    private RoomService roomService;

    @Autowired
    private ReservationService reservationService;
    @Autowired
    private CommentService commentService;

    @RequestMapping("/login")
    @ResponseBody
    public ResultUtil login(String username, String password, HttpServletRequest request, HttpSession session)
    {
        User user=userService.login(username,EncryptUtil.encrypt(password));

        if(user!=null)
        {
            Integer status=user.getStatus();
            if(status==0)
            {
                 return ResultUtil.error("对不起，此用户被禁用，请联系超级管理员");
            }
            Admin admin=new Admin();
            session.setAttribute("user",admin); //拦截器中用 跳过拦截器
            session.setAttribute("hotel_user",user);
            session.setAttribute("usertype",2);//代表会员登陆
            session.setAttribute("hotel_user_id",user.getUser_id());
            session.setAttribute("hotel_user_photo",user.getPhoto_path());//获得头像图片名
            return ResultUtil.ok();
        }
        else
        {
            return ResultUtil.error("用户名或者密码错误，请重新输入");
        }

    }

    @RequestMapping("/getMenus")
    @ResponseBody
    public List<Menu> getMenus(HttpSession session)
    {
        //System.out.println("学生获取菜单进来了");
        List<Menu> menus=null;
        User user= (User) session.getAttribute("hotel_user");
        User user1=userService.getUserById(user.getUser_id());
        if(user1!=null)
        {
            menus=userService.getMenus(user1);
        }
        return menus;
    }

    @RequestMapping("/logOut")
    public ModelAndView logOut(ModelAndView modelAndView, HttpSession session)
    {
        session.invalidate();
        modelAndView.setViewName("redirect:/index.jsp");
        return modelAndView;
    }


   

    //历史订单
    @RequestMapping("/orderList")
    public String orderList()
    {
        return "/jsp/user/orderList";
    }
    @RequestMapping("/getOrderList")
    @ResponseBody
    public ResultUtil getOrderList(Integer page,Integer limit,HttpSession session)
    {
        //获取历史订单
        Integer user_id= (Integer) session.getAttribute("hotel_user_id");
        return reservationService.getReservationOldList(page,limit,user_id);
    }

    //预定的订单
    @RequestMapping("/nowOrderList")
    public String nowOrderList()
    {
        return "/jsp/user/nowOrderList";
    }

    @RequestMapping("/getNowOrderList")
    @ResponseBody
    public ResultUtil getNowOrderList(Integer page,Integer limit,HttpSession session)
    {
        //获取历史订单
        Integer user_id= (Integer) session.getAttribute("hotel_user_id");
        return reservationService.getReservationNowList(page,limit,user_id);
    }

    @RequestMapping("/cancel/{room_info_id}")
    @ResponseBody
    public ResultUtil cancel(@PathVariable Integer room_info_id,
                          HttpSession session) throws ParseException {
            try
            {
                Integer user_id= (Integer) session.getAttribute("hotel_user_id");

                RoomInfo roomInfo=roomService.getRoomById(room_info_id);
                roomInfo.setStatus(0); //设置为空闲状态
                roomService.updRoom(roomInfo);
                reservationService.deleteReservation(user_id,room_info_id);
                return ResultUtil.ok();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return ResultUtil.error();
            }
    }

}
